package org.example.aidemo.service.impl;

import org.example.aidemo.mapper.UserCredentialsMapper;
import org.example.aidemo.pojo.UserCredentials;
import org.example.aidemo.service.UserCredentialsService;
import org.example.aidemo.service.UserService;
import org.example.aidemo.utils.AESUtils;
import org.example.aidemo.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class UserCredentialsServiceImpl implements UserCredentialsService {
    @Autowired
    private UserCredentialsMapper userCredentialsMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private SecurityUtils securityUtils;
    @Override
    public UserCredentials addUserCredentials(String platformName, String platformUrl, String accountUsername, String encryptedPassword) {
        Long userid=userService.findByUsername(securityUtils.getCurrentUsername()).getId();
        Timestamp createdAt=new Timestamp(System.currentTimeMillis());
        Timestamp updatedAt=new Timestamp(System.currentTimeMillis());
        String encryptedPwd= AESUtils.encrypt(encryptedPassword);
        UserCredentials userCredentials=new UserCredentials(userid,platformName, platformUrl, accountUsername, encryptedPwd,createdAt,updatedAt);
        userCredentialsMapper.addUserCredentials(userCredentials);
        return userCredentials;
    }
    @Override
    public List<UserCredentials> getUserCredentialsByUserId(Long UserId){
        List<UserCredentials> userCredentials=userCredentialsMapper.getUserCredentialsByUserId(UserId);
        return userCredentials;
    }
    @Override
    public void deleteUserCredentials(List<Long> ids){
        for(Long id : ids){
            userCredentialsMapper.deleteUserCredentials(id);
        }
    }
}
